package com.palm.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.palm.dao.entity.Car;
import com.palm.dao.entity.Location;
import com.palm.web.response.BaseResponse;
import com.palm.web.response.CarLocationQueryResponse;
import com.palm.web.util.DataPool;

@Controller
public class CarController {

	private static Logger logger = LoggerFactory.getLogger(CarController.class);

	@RequestMapping(value = "/car_list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("title", "车辆管理");
		model.addAttribute("groupTitle", "车辆列表");
		List<Car> carList = new ArrayList<Car>() {

			private static final long serialVersionUID = 1L;
			{
				add(new Car("沪A12345", "奔驰", "12344556777", "红色"));
				add(new Car("沪A78623", "宝马", "97363738333", "白色"));
			}
		};

		model.addAttribute("carList", carList);

		return "car_list";
	}

	@ResponseBody
	@RequestMapping(value = "/car_location_query", method = RequestMethod.GET)
	public CarLocationQueryResponse carLocationQuery() {

		Location location=DataPool.Instance().get();
		if(location==null)
		{
			return new CarLocationQueryResponse("失败", -1,location );
		}
		return new CarLocationQueryResponse("成功", 0,location );
	}

	@ResponseBody
	@RequestMapping(value = "/car_location_create", method = RequestMethod.POST)
	public BaseResponse carLocationCreate(double longitude, double latitude, String plateNum) {

		DataPool.Instance().add(new Location(longitude, latitude, plateNum));
		return new BaseResponse("成功", 0);
	}

}
